package com.example;


import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.pojo.Brand;


import javax.sql.DataSource;
import java.io.FileInputStream;

import java.io.FileNotFoundException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Properties;

/**
 * 品牌数据的crud操作
 */
public class BrandTest {


    public static void testSelectAll() throws Exception {
        //.加载配置文件
        Properties prop = new Properties();
        prop.load(new FileInputStream("jdbc_demo/src/druid.properties"));
        //获取连接池对象

        DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);

        Connection connection = dataSource.getConnection();

        String sql = "select * from tb_brand";

        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();

        ArrayList<Brand> brands = new ArrayList<>();

        //处理结果
        Brand brand = null;
        while (resultSet.next()){
            brand = new Brand();

            //获取数据
            int id = resultSet.getInt("id");
            String brandName = resultSet.getString("brand_name");
            String companyName = resultSet.getString("company_name");
            int ordered = resultSet.getInt("ordered");
            String description = resultSet.getString("description");
            int status = resultSet.getInt("status");

            brand.setId(id);
            brand.setBrandName(brandName);
            brand.setCompanyName(companyName);
            brand.setOrdered(ordered);
            brand.setDescription(description);
            brand.setStatus(status);

            //装在入集合中
            brands.add(brand);
        }

        System.out.println(brands);

        //释放资源
        resultSet.close();
        preparedStatement.close();
        connection.close();

    }

    public static void testAdd() throws Exception {
        //接受参数
        String brandName = "Air Jordan";
        String companyName = "Nike";
        int  ordered = 3;
        String description = "just do it 乔丹";
        int status = 1;

        //.加载配置文件
        Properties prop = new Properties();
        prop.load(new FileInputStream("jdbc_demo/src/druid.properties"));
        //获取连接池对象

        DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);

        Connection connection = dataSource.getConnection();

        String sql = "insert into tb_brand(brand_name,company_name,ordered,description,status) values(?,?,?,?,?)";

        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,brandName);
        preparedStatement.setString(2,companyName);
        preparedStatement.setInt(3,ordered);
        preparedStatement.setString(4,description);
        preparedStatement.setInt(5,status);

       int i = preparedStatement.executeUpdate();
       if(i > 0){
           System.out.println("新增成功");
       }else{
           System.out.println("新增失败");
       }
    }

    public static void main(String[] args) throws Exception {
       //BrandTest.testSelectAll();
        BrandTest.testAdd();
    }

}
